﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Nirvana
{
    [CustomEditor(typeof(Footprint))]
    public class FootprintEditor : Editor
    {
        private SerializedProperty material;
        private SerializedProperty uv1;
        private SerializedProperty uv2;
        private SerializedProperty uv3;
        private SerializedProperty uv4;
        private SerializedProperty size;
        private SerializedProperty duration;
        private SerializedProperty fadeout;
        private SerializedProperty groundOffset;

        private Material curMaterial;
        private void OnEnable()
        {
            SerializedObject serializedObject = base.serializedObject;
            this.material = serializedObject.FindProperty("material");
            this.uv1 = serializedObject.FindProperty("uv1");
            this.uv2 = serializedObject.FindProperty("uv2");
            this.uv3 = serializedObject.FindProperty("uv3");
            this.uv4 = serializedObject.FindProperty("uv4");
            this.size = serializedObject.FindProperty("size");
            this.duration = serializedObject.FindProperty("duration");
            this.fadeout = serializedObject.FindProperty("fadeout");
            this.groundOffset = serializedObject.FindProperty("groundOffset");
        }


        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.material, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.size, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.duration, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.fadeout, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.groundOffset, new GUILayoutOption[0]);
            EditorGUILayout.Space();
            GUILayout.Label("Atlas UV:", new GUILayoutOption[0]);
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(this.uv1, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                this.uv1.vector2Value = new Vector2(Mathf.Clamp01(this.uv1.vector2Value.x), Mathf.Clamp01(this.uv1.vector2Value.y));
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(this.uv2, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                this.uv2.vector2Value = new Vector2(Mathf.Clamp01(this.uv2.vector2Value.x), Mathf.Clamp01(this.uv2.vector2Value.y));
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(this.uv3, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                this.uv3.vector2Value = new Vector2(Mathf.Clamp01(this.uv3.vector2Value.x), Mathf.Clamp01(this.uv3.vector2Value.y));
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(this.uv4, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                this.uv4.vector2Value = new Vector2(Mathf.Clamp01(this.uv4.vector2Value.x), Mathf.Clamp01(this.uv4.vector2Value.y));
            }
            DrawPreView();
            base.serializedObject.ApplyModifiedProperties();
        }

        private void DrawPreView()
        {
            GUILayout.BeginVertical(GUI.skin.textArea, GUILayout.Width(256f));
            Material mat = this.material.objectReferenceValue as Material;
            if(mat != null && mat.mainTexture != null)
            {
                GUILayout.Label(mat.mainTexture, GUILayout.Width(256f), GUILayout.Height(256f));
            }
            else
            {
                GUILayout.Label(string.Empty, GUILayout.Width(256f), GUILayout.Height(256f));
            }

            Rect lastRect = GUILayoutUtility.GetLastRect();
            GUILayout.EndVertical();

            Vector2 uvpos1 = new Vector2(lastRect.x + lastRect.width * this.uv1.vector2Value.x, lastRect.y + lastRect.height * this.uv1.vector2Value.y);
            Vector2 uvpos2 = new Vector2(lastRect.x + lastRect.width * this.uv2.vector2Value.x, lastRect.y + lastRect.height * this.uv2.vector2Value.y);
            Vector2 uvpos3 = new Vector2(lastRect.x + lastRect.width * this.uv3.vector2Value.x, lastRect.y + lastRect.height * this.uv3.vector2Value.y);
            Vector2 uvpos4 = new Vector2(lastRect.x + lastRect.width * this.uv4.vector2Value.x, lastRect.y + lastRect.height * this.uv4.vector2Value.y);

            this.DrawUVAtlas(uvpos1.x, uvpos1.y, uvpos2.x, uvpos2.y, Color.green, false, default(Rect));
            this.DrawUVAtlas(uvpos2.x, uvpos2.y, uvpos4.x, uvpos4.y, Color.green, false, default(Rect));
            this.DrawUVAtlas(uvpos4.x, uvpos4.y, uvpos3.x, uvpos3.y, Color.green, false, default(Rect));
            this.DrawUVAtlas(uvpos3.x, uvpos3.y, uvpos1.x, uvpos1.y, Color.green, false, default(Rect));
        }


        private void DrawUVAtlas(float x0,float y0,float x1,float y1,Color color,bool limit,Rect rect = default(Rect))
        {
            if (this.curMaterial == null)
            {
                this.curMaterial = new Material(Shader.Find("Unlit/Color"));
                this.curMaterial.hideFlags = HideFlags.HideAndDontSave;
                this.curMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
            }
            this.curMaterial.color = color;
            this.curMaterial.SetPass(0);
            if (limit)
            {
                if(x0< rect.x) x0 = rect.x;
                if(x0 > rect.width) x0 = rect.width;

                if (y0 < rect.y) y0 = rect.y;
                if (y0 > rect.height) y0 = rect.height;

                if (x1 < rect.x) x1 = rect.x;
                if (x1 > rect.width) x1 = rect.width;

                if (y1 < rect.y) y1 = rect.y;
                if (y1 > rect.height) y1 = rect.height;
            }

            GL.Begin(1);
            GL.Color(color);
            GL.Vertex3(x0, y0, 0);
            GL.Vertex3(x1, y1, 0);
            GL.End();
        }

    }
}

